package org.eenie.kdassistant.net.yd_api;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.yunda.crypt.EDOutputData;
import com.yunda.crypt.NativeEncryption;
import com.yunda.crypt.security.EncryptionFactory;

import java.util.Date;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Eenie on 2017/8/28.
 * Email: eeniework@gmail.com
 * Des:
 */

public class YDBaseRequest {



    private String sign_method = "yd_md5";
    private String req_time;
    private String data;
    private String action;
    private String appver = "5.6.0";
    private String version;
    private String token;
    private Boolean encrypt = true;

    private Object dataObj;


    public YDBaseRequest(String action, Object param, String token, String version, Boolean encrypt) {

        this.version = version;
        this.token = token;
        this.encrypt = encrypt;
        this.dataObj = param;
        this.action = action;
    }
    public YDBaseRequest(String action,Object param, String token, String version) {
        this.version = version;
        this.token = token;
        this.dataObj = param;
        this.action = action;
    }

    public YDBaseRequest() {

    }

    public String getSign_method() {
        return sign_method;
    }

    public void setSign_method(String sign_method) {
        this.sign_method = sign_method;
    }

    public String getReq_time() {
        return req_time;
    }

    public void setReq_time(String req_time) {
        this.req_time = req_time;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getAppver() {
        return appver;
    }

    public void setAppver(String appver) {
        this.appver = appver;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }


    public RequestBody getReqBody() {
       if (encrypt){
           EDOutputData edOutputData = new EDOutputData();
           NativeEncryption.JNI_IEncrypt(JSON.toJSONString(dataObj), edOutputData);
           data = edOutputData.GetData();
       }else {
           data = JSON.toJSONString(dataObj);
       }

        StringBuffer paramBf = new StringBuffer();
        paramBf.append("sign_method=")
                .append(sign_method)
                .append("&req_time=")
                .append(new Date().getTime())
                .append("&data=")
                .append(data)
                .append("&action=")
                .append(action)
                .append("&appver=")
                .append(appver)
                .append("&version=")
                .append(version);

        if (!TextUtils.isEmpty(token)) {
            paramBf.append("&token=")
                    .append(token);
        }

        String res = EncryptionFactory.getEncryption("yd_md5").encodeRequest(paramBf.toString(), null, "UTF-8");
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), res);
        return body;
    }

}
